package brainsplash.gyrusscope.game;

import java.util.ArrayList;
import java.util.Random;

import brainsplash.gyrusscope.framework.gl.TextureRegion;

public class ParticleSystem {
	private TextureRegion sprite;
	private int radius;
	private float speed;
	private int center_x;
	private int center_y;
	private float distanceFromCenter;
	public int amount;
	private int burst;
	private int spread;
	private int wait;
	private int lifetime;
	private float sizeSpeed;
    private int[] alarm = {0};
    public ArrayList<Particle> particles = new ArrayList<Particle>();
    public Random randomGenerator = new Random();
    
    public ParticleSystem(TextureRegion sprite, int radius, int center_x, int center_y, float distanceFromCenter,
    		float speed, int amount, int burst, int spread, int wait, int lifetime, float sizeSpeed) {
    	this.sprite = sprite;
    	this.radius = radius;
    	this.center_x = center_x;
    	this.center_y = center_y;
    	this.distanceFromCenter = distanceFromCenter;
    	this.speed = speed;
    	this.amount = amount;
    	this.burst = burst;
    	this.spread = spread;
    	this.wait = wait;
    	this.lifetime = lifetime;
    	this.sizeSpeed = sizeSpeed;
    }
    
    public void update() {
	    alarm[0] += 1;
	    if (alarm[0] == wait && amount != 0) {
	    	float dir = randomGenerator.nextFloat() * 360f;
	    	
	    	for (int i = 0; i < burst; i++) {
	    		particles.add(new Particle(sprite, radius, center_x, center_y, distanceFromCenter, dir + (spread / burst) * i, speed, lifetime, sizeSpeed));
	    	}
	    	
	    	if (amount != -1) {amount -= burst; if (amount < 0) {amount = 0;}}
	    	alarm[0] = 0;
	    }
	    
		for (int i=0; i < particles.size(); i++) {
			Particle p = particles.get(i);
			p.update();
			if (p.distanceFromCenter >= 200 || p.lifetime <= 0) {particles.remove(p);}
		}
    }
}
